#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

void lexical(FILE *fp) {
	char x = getc(fp);
	num = 0;
	strcpy(id, "\0");
	    char temp;
	while(x==' '||x=='\t'||x=='\n')x=getc(fp);
    if(isdigit(x))
    {
        while(isdigit(x))
        {
            num=num*10+x-'0';
            x=getc(fp);
        }
        token=11;
        ungetc(x,fp);
        return;
    }
    else if(isalpha(x))
    {
        while(isalpha(x)||isdigit(x))
        {

            sprintf(id,"%s%c",id,x);
            x=getc(fp);
        }
        token=10;
        ungetc(x,fp);

        if(!strcmp(id,"for"))token=1;
        else if(!strcmp(id,"if"))token=2;
        else if(!strcmp(id,"then"))token=3;
        else if(!strcmp(id,"else"))token=4;
        else if(!strcmp(id,"while"))token=5;
        else if(!strcmp(id,"do"))token=6;
        else return;
    }
    else if(x=='+')
    {
        token=13;
        strcpy(id,"+");
    }
    else if(x=='-')
    {
        token=14;
        strcpy(id,"-");
    }
    else if(x=='*')
    {
        token=15;
        strcpy(id,"*");
    }
    else if(x=='/')
    {
        token=16;
        strcpy(id,"/");
    }
    else if(x==':')
    {
        temp=getc(fp);
        if(temp=='=')
        {
            token=18;
            strcpy(id,":=");
        }
        else
        {
            token=17;
            strcpy(id,":");
            ungetc(temp,fp);
        }
    }
	else if(x=='>')
    {
        temp=getc(fp);
        if(temp=='=')
        {
            token=24;
            strcpy(id,">=");
        }
        else
        {
            token=23;
            strcpy(id,">");
            ungetc(temp,fp);
        }
    }
    else if(x=='<')
    {
        temp=getc(fp);
        if(temp=='=')
        {
            token=22;
            strcpy(id,"<=");
        }
        else if(temp=='>')
        {
            token=21;
            strcpy(id,"<>");
        }
        else
        {
            token=20;
            strcpy(id,"<");
            ungetc(temp,fp);
        }
    }
    else if(x=='=')
    {
        token=25;
        strcpy(id,"=");
    }
    else if(x==';')
    {
        token=26;
        strcpy(id,";");
    }
    else if(x=='(')
    {
        token=27;
        strcpy(id,"(");
    }
    else if(x==')')
    {
        token=28;
        strcpy(id,")");
    }
    else if(x=='#')
    {
        token=0;
        strcpy(id,"#");
    }
	//process x
	//process each regex here
	//store the result in global variables id, num, or token
	//in case of backtrack, use
	//ungetc(x, fp);

}

int main(int argc, const char *argv[])
{
	if(argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch(token) {
			case 10: printf("(id, %s)\n", id); break;
			case 11: printf("(num, %d)\n", num); break;
			case -1: printf("error!\n"); break;
			default: printf("(%d, %s)\n", token, id);
		}
	} while(token != 0);


	fclose(fp);
	return 0;
}
